package ua.kpi.oblamskyiv.parser;

import ua.kpi.oblamskyiv.io.exception.IllegalAnswerFormatException;
import ua.kpi.oblamskyiv.testcase.Answers;
import ua.kpi.oblamskyiv.testcase.answer.Answer;
import ua.kpi.oblamskyiv.testcase.question.Question;

import java.util.ArrayList;
import java.util.List;

public class TextAnswersParser implements AnswersParser {
    private static final String COMMA_WITH_OPTIONAL_SPACE = ",(\\s)*";

    @Override
    public Answers parse(Question question, String rawString)
            throws IllegalAnswerFormatException {
        String[] answers = rawString.split(COMMA_WITH_OPTIONAL_SPACE);
        List<Answer> answersByStrings = new ArrayList<Answer>();

        for (String answerString : answers) {
            answersByStrings.add(new Answer(answerString));
        }
        return new Answers(answersByStrings);
    }
}
